package com.cyys.controller;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cyys.entity.*;
import com.cyys.mapper.CategoryGoodsMapper;
import com.cyys.response.Result;
import com.cyys.service.CategoryGoodsService;
import com.cyys.service.CategoryService;
import com.cyys.service.HomeImgService;
import com.cyys.service.TakeoutService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @description CategoryGoodsController
 * @date 2022-05-26 15:13
 */
@RestController
@RequestMapping("/CategoryGoods")
public class CategoryGoodsController {
    @Autowired
    private CategoryGoodsService categoryGoodsService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private TakeoutService takeoutService;


    @GetMapping("/categoryList")
    public Result categoryList() {

        List<CategoryGoods> CategoryGoods = categoryGoodsService.list();
        return Result.ok().data("CategoryGoods", CategoryGoods);
    }


    /**
     * 多表查询id
     *
     * @param cid
     * @return
     */
    @GetMapping("/category_goods")
    public Result CategoryGoods(Integer cid) {

        CategoryGoods ca = new CategoryGoods();
        ca.setCategoryid(cid);
        List<CategoryGoods> CategoryGoods = categoryGoodsService.findCategoryGoodsList(ca);
        return Result.ok().data("CategoryGoods", CategoryGoods);
    }

    /**
     * 查询id
     *
     * @return
     */
    @GetMapping("/categoryGoodsID")
    public Result category_goods_ID(Integer id) {

        CategoryGoods categoryGoods = categoryGoodsService.getById(id);

        List<Takeout> takeout = takeoutService.listById(categoryGoods.getId());

        List<pei> list1=new ArrayList<>();
        for (Takeout takeout1:takeout){
            String value = takeout1.getValue();
            String[] values={};
            String value1=value.substring(1,value.length()-1);
            List<String> list=new ArrayList<>();
            values=value1.split(",");
            for (String ss:values){
                list.add(ss.substring(1,ss.length()-1));
            }
            pei pei=new pei();
            pei.setId(takeout1.getId());
            pei.setName(takeout1.getName());
            pei.setValue(list);
            list1.add(pei);
        }
        categoryGoods.setValues(list1);
        return Result.ok().data("CategoryGoods", categoryGoods);
    }


    /**
     * 多表查询label    推荐——新品
     *
     * @param name
     * @return
     */
    @ApiOperation(value = "category_goods名字name模糊查询")
    @GetMapping("/selectName")
    public Result CategoryGoodsRecommend(String name) {

        CategoryGoods ca = new CategoryGoods();
        ca.setName(name);
        List<CategoryGoods> list = categoryGoodsService.findCategoryGoodsListRecommend(ca);
        return Result.ok().data("list", list);
    }

    @GetMapping("/categoryGoodsList")
    public Result categoryGoodsList(Integer storeId) {
        String category1 = "新品尝鲜";
        List<Category> Category = categoryService.getListName(category1, storeId);
        List<CategoryGoods> CategoryGoods = categoryGoodsService.list();
        for (Category li : Category) {
            List<CategoryGoods> category = new ArrayList<>();
            System.out.println("Category.Id" + li.getId());
            for (CategoryGoods l : CategoryGoods) {
                if (l.getLabel() != null && l.getLabel().equals("0") && l.getStoreId() == storeId) {
                    System.out.println("CategoryGoods.Categoryid" + l.getCategoryid());
                    category.add(l);
                }

                if (l.getCategoryid() == li.getId()) {
                    category.add(l);
                }
            }
            li.setCategoryGoods(category);
        }
        return Result.ok().data("Category", Category);
    }

    @GetMapping("/categoryGoodsListID")
    public Result categoryGoodsListID(Integer cid) {
        List<Category> Category = categoryService.getById(cid);
        CategoryGoods ca = new CategoryGoods();
        ca.setCategoryid(cid);
        List<CategoryGoods> CategoryGoods = categoryGoodsService.findCategoryGoodsList(ca);
        for (Category li : Category) {
            System.out.println("Category.Id" + li.getId());
            List<CategoryGoods> category = new ArrayList<>();
            for (CategoryGoods l : CategoryGoods) {
                if (li.getId() == cid) {
                    category.add(l);
                }
            }

            li.setCategoryGoods(category);
        }
        return Result.ok().data("Category", Category);
    }



    /**
     * 多表查询 name和 categoryId
     * 搜索
     * @param name
     * @return
     */
    @ApiOperation(value = "搜索")
    @GetMapping("/search")
    public Result search(String name,Integer categoryId) {

        CategoryGoods ca = new CategoryGoods();
        ca.setName(name);
        ca.setCategoryid(categoryId);
        List<CategoryGoods> list = categoryGoodsService.search(ca);
        return Result.ok().data("list", list);
    }


}
